/**
 * Created by ZFL on 2018/1/10.
 */

var vm = new Vue({
    el:'#saleEle',
    data:{
        childId:'',          //确定显示的是哪个子页面
        childData:{},       //子页面中的数据
        logCompany:[],      //物流公司数组
        logNameObj:{},      //物流信息
        logSelect:'',       //选中的物流公司
        logNum:'',          //输入的物流单号
        address:{},         //收货地址
        appInd:0,           //不是内嵌的APP
        sureYesNo:false    //确认是否点击确认寄回了 默认是否
    },
    methods:{
        //返回主页面
        returnPayPage:function(type){
            var _this = this;
            if(addInd){
                //表示是内嵌的APP
                $('.headerNav').show();
            }
            $('body').css('background','#EEE');
            _this.childId = type;
        },
        //查看物流公司和输入物流单号
        selectPageLog:function(type){
            var _this = this;
            if(!_this.sureYesNo){
            if(addInd){
                //表示是内嵌的APP
                $('.headerNav').hide();
            }
            _this.childId = type;
            if(type == 6){
                //物流单号
                $('body').css('background','#FFF');
            }else {
                //物流公司
                $('body').css('background','#EEE');
            }
            }
        },
        //设置物流公司
        selectLogFun:function(item){
            var _this = this;
            _this.logNameObj.companyName = item.companyName;    //中文名字
            _this.logNameObj.companyNo = item.companyNo;
            _this.logNameObj.id = item.id;
            _this.childId = 2;
        },
        //确认输入的物流单号
        logSureFun:function(){
            var _this = this;
            //判断物流单号是否合法
            if(_this.logNum != ''){
                //必须输入物流单号
                _this.childId = 2;
            }
        }
    }
});

var userId = 0;
var afterSalesId = '';   //售后ID
var afterSn = '';   //物流单号
var addInd = 0;     //表示不是内嵌App
function PageInit(){
    if(com_obj.userId != ''){
        userId = com_obj.userId;
        getSaleInfData();       //获取售后订单详情数据
    }
    //初始化物流公司地址
    vm.logNameObj = {};
    vm.logNameObj.companyName = '';
    vm.logNameObj.companyNo = '';
    vm.logNameObj.id = '';
    //初始化收货地址
    vm.address = {};
    vm.address.linkMan = '';
    vm.address.address = '';
    vm.address.area = '';
    vm.address.city = '';
    vm.address.mobile = '';
    vm.address.province = '';
    // status 0平台拒绝 1待处理 2已同意 3已完成 4已拒绝

}
function appInit(){
    addInd = 1;     //表示是内嵌App
    vm.appInd = 1;      //表示是内嵌App
    PageInit();
}
var goodImgPath=''
//获取售后详情数据
function getSaleInfData(){
    $.ajaxHandler({
        model: function () {
            var obj = {};
            obj.afterSalesId = $.getPara("afterSalesId");
            return obj;
        },
        visibleTime: 0,
        dataType: "json",
        approve: true,
        visible:'hide',
        api: 'yg_baijiaapporder/afterSales/ygBaFindAfterSalesById',
        success: function (result) {
            //console.log(result);
            if(result.code == 10000){
                var data = result.data;
                afterSalesId = data.afterSalesId;//售后ID
                afterSn = data.afterSn;//售后服务号
                goodImgPath=data.goodImg;   //店铺图片
                if(data.status == 1){//0平台拒绝 1待处理 2已同意 3已完成 4已拒绝
                    vm.childId = 1;
                }else if(data.status == 2 && data.isRefund ==1){//1退款 2退货退款
                    vm.childId = 6;
                }else if(data.status == 2 && data.isRefund ==2){//1退款 2退货退款
                    vm.childId = 2;
                }else if(data.status == 3 && data.isRefund ==1){//1退款 2退货退款
                    vm.childId = 3;
                }else if(data.status == 3 && data.isRefund ==2){//1退款 2退货退款
                    vm.childId = 5;
                }else if(data.status == 4){
                    vm.childId = 4;
                }
                vm.childData = {};
                vm.childData = data;
                if(!data.address){
                    //没有地址的情况下
                }else {
                    vm.address = data.address;
                }
                vm.childData.salePeice = divideNumber(vm.childData.salePeice,100);      //商品单价
                vm.childData.refundMoney = divideNumber(vm.childData.refundMoney,100);  //退款金额
                vm.childData.reFundType = '';   //退款类型
                if(vm.childData.isRefund == 1){
                    vm.childData.reFundType = '退款';
                }else if(vm.childData.isRefund == 2){
                    vm.childData.reFundType = '退货退款';
                }
                //3张图片
                vm.childData.imgProofS = [];
                if(vm.childData.imgProof1!=''){
                    vm.childData.imgProofS.push(vm.childData.imgProof1);
                }
                if(vm.childData.imgProof2!=''){
                    vm.childData.imgProofS.push(vm.childData.imgProof2);
                }
                if(vm.childData.imgProof3!=''){
                    vm.childData.imgProofS.push(vm.childData.imgProof3);
                }
                console.log(vm.childData.imgProofS)
                //物流公司和物流单号 已经填写物流公司和单号的情况下
                if(vm.childData.courierCompany != ''){
                    //在有物流公司和物流单号的情况下
                    vm.logNameObj.companyName = vm.childData.courierCompany;        //物流公司中文名字
                    vm.logNameObj.companyNo = vm.childData.courierCompanyEn;        //物流公司英文名字
                    vm.logNum = vm.childData.courierNumber;                         //物流单号
                    vm.sureYesNo = true;                                            //不可点击
                }
                //只有在退货退款中并且没有物流信息的情况下  才会查看物流公司
                if(vm.childId == 2 && !vm.sureYesNo){
                    getLogCom();        //获取物流公司
                }

                //console.log(vm.childData);
            }else {
                YDUI.dialog.toast(result.message, 'error', 1000);
            }
        }
    })
}

//获取物流公司数据
function getLogCom(){
    if(vm.logCompany.length == 0){
        $.ajaxHandler({
            model: function () {
                return {};
            },
            visibleTime: 0,
            dataType: "json",
            approve: true,
            visible:'hide',
            api: 'yg_baijiaapporder/order/listExpreseCompany',
            success: function (result) {
                //console.log(result);
                if(result.code == 10000){
                    var data = result.data;
                    vm.logCompany = [];
                    if(data.length >= 0){
                        vm.logCompany = data;
                    }
                }else {
                    YDUI.dialog.toast(result.message, 'error', 1000);
                }
            }
        })
    }
}

//确认寄回
function sureReFun(e){
    console.log(12)
    if(vm.logNameObj.companyName !='' && vm.logNum!=''){
        //确定已经选择了物流公司并输入了物流单号
        $.ajaxHandler({
            model: function () {
                var obj = {};
                obj.afterSalesId = afterSalesId;
                obj.courierCompany = vm.logNameObj.companyName;     //物流公司中文名字
                obj.courierCompanyEn = vm.logNameObj.companyNo;     //物流公司英文
                obj.courierNumber = vm.logNum;                      //快递单号
                return obj;
            },
            visibleTime: 0,
            dataType: "json",
            approve: true,
            visible:'hide',
            api: 'yg_baijiaapporder/afterSales/ygBaAddCourierInformation',
            success: function (result) {
                if(result.code == 10000){
                    //确认已经提交寄回
                    vm.sureYesNo = true;

                }else {
                    YDUI.dialog.toast(result.message, 'error', 1000);
                }
            }
        });
    }else if(vm.logNameObj.companyName!=''){
        YDUI.dialog.toast('请填写物流公司名称',1000)
    }else if(vm.logNum!=''){
        YDUI.dialog.toast('请填写物流单号',1000)
    }else{
        YDUI.dialog.toast('请填写物流信息',1000)
    }
    console.log(vm.logNameObj.companyName!='')
}

//查看物流
function goOrderPage(){
    //afterSn = afterSn.split('A')[1];
    //console.log(afterSn);
    window.location.href ='../order/check_logistics.html?orderSn='+afterSn+'&company='+vm.logNameObj.companyName+'&no='+vm.logNum+'&goodImgPath='+goodImgPath;
}

//返回
function returnPage(){
    history.go(-1);
}

//待处理  倒计时
function CountdownFun(){
    var starttime = new Date("2017/6/2");
    var timeInter = setInterval(function () {
        var nowtime = new Date();
        var time = starttime - nowtime;
        var day = parseInt(time / 1000 / 60 / 60 / 24);
        var hour = parseInt(time / 1000 / 60 / 60 % 24);
        var minute = parseInt(time / 1000 / 60 % 60);
        var seconds = parseInt(time / 1000 % 60);
        $('.timespan').html(day + "天" + hour + "小时" + minute + "分钟" + seconds + "秒");
        if(day <= 0 && hour <= 0 && minute <= 0 && seconds <= 0){
            //结束了 清除倒计时
            day = 0;
            hour = 0;
            minute = 0;
            seconds = 0;
            $('.timespan').html(day + "天" + hour + "小时" + minute + "分钟" + seconds + "秒");
            clearInterval(timeInter);
        }
    }, 1000);
}


